package com.yaozheng.vocationaltraining.adapter.paper;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.yaozheng.vocationaltraining.activity.BaseActivity;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.question.item.ItemBaseQuestionView;
import com.yaozheng.vocationaltraining.view.question.item.ItemMultipleChoiceQuestionView_;
import com.yaozheng.vocationaltraining.view.question.item.ItemSingleChoiceQuestionView_;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 一项题目选项设备器
 */
public class ItemQuestionListAdapter extends BaseAdapter implements OnCancelSelect1 {
    JSONObject itemData;
    JSONArray options;
    String[] answerCodes;
    Context context;
    ItemBaseQuestionView lastItemBaseQuestionView;

    public ItemQuestionListAdapter(JSONObject itemData, Context context) {
        this.context = context;
        answerCodes = new String[]{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        setItemData(itemData);
    }

    @Override
    public int getCount() {
        int size = TypeUtils.getJsonArraySize(options);
        if (size > answerCodes.length) {
            return answerCodes.length;
        }
        return size;
    }

    public void setItemData(JSONObject itemData) {
        this.itemData = itemData;
        options = TypeUtils.getJsonArray(itemData, "options");
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }


    @Override
    public int getItemViewType(int position) {
        boolean multiple = TypeUtils.getJsonBoolean(itemData, "multiple");
        if (multiple) {
            //多选
            return 1;
        }
        return super.getItemViewType(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        boolean multiple = TypeUtils.getJsonBoolean(itemData, "multiple");
        ItemBaseQuestionView itemBaseQuestionView;
        if (convertView == null) {
            if (multiple) {
                itemBaseQuestionView = ItemMultipleChoiceQuestionView_.build(context);
            } else {
                itemBaseQuestionView = ItemSingleChoiceQuestionView_.build(context);
            }
            itemBaseQuestionView.setItemData(itemData);
            itemBaseQuestionView.setOnCancelSelect(this);
        } else {
            itemBaseQuestionView = (ItemBaseQuestionView) convertView;
        }
        String itemOption = TypeUtils.getJsonString(options, position);
        String answerCode = answerCodes[position];
        itemBaseQuestionView.setPosition(position);
        itemBaseQuestionView.setAnswerCode(answerCode);
        itemBaseQuestionView.setOptionsContent(answerCode + "：" + (itemOption));
        itemBaseQuestionView.setAnswer(TypeUtils.getJsonString(itemData, "answer"));
        JSONArray optionsCard = TypeUtils.getJsonArray(itemData, "optionsCard");
        itemBaseQuestionView.optionsCard(optionsCard);
        return itemBaseQuestionView;
    }

    public void cancelSelect(ItemBaseQuestionView itemBaseQuestionView) {
        if (lastItemBaseQuestionView != null) {
            lastItemBaseQuestionView.cancelSelect();
        }
        lastItemBaseQuestionView = itemBaseQuestionView;
    }


}

